package com.ruoyi.shop.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.ShopCoupon;
import com.ruoyi.shop.service.IShopCouponService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 * 
 * @author Alan Zhang
 * @date 2021-09-18
 */
@Controller
@RequestMapping("/coupon/coupon")
public class ShopCouponController extends BaseController
{
    private String prefix = "coupon/coupon";

    @Autowired
    private IShopCouponService shopCouponService;

    @RequiresPermissions("coupon:coupon:view")
    @GetMapping()
    public String coupon()
    {
        return prefix + "/coupon";
    }

    /**
     * 查询优惠券列表
     */
    @RequiresPermissions("coupon:coupon:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShopCoupon shopCoupon)
    {
        startPage();
        List<ShopCoupon> list = shopCouponService.selectShopCouponList(shopCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("coupon:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopCoupon shopCoupon)
    {
        List<ShopCoupon> list = shopCouponService.selectShopCouponList(shopCoupon);
        ExcelUtil<ShopCoupon> util = new ExcelUtil<ShopCoupon>(ShopCoupon.class);
        return util.exportExcel(list, "优惠券数据");
    }

    /**
     * 新增优惠券
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券
     */
    @RequiresPermissions("coupon:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopCoupon shopCoupon)
    {
        return toAjax(shopCouponService.insertShopCoupon(shopCoupon));
    }

    /**
     * 修改优惠券
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShopCoupon shopCoupon = shopCouponService.selectShopCouponById(id);
        mmap.put("shopCoupon", shopCoupon);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("coupon:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShopCoupon shopCoupon)
    {
        return toAjax(shopCouponService.updateShopCoupon(shopCoupon));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("coupon:coupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopCouponService.deleteShopCouponByIds(ids));
    }
}
